/**
 * Created by zlp on 2017-12-14.
 */

var userMock=require("../mock/data/user");


let _Users=userMock.Users();
exports.listPage=function(req,res,next){
    let {page, name} = req.body;
    let mockUsers = _Users.filter(user => {
        if (name && user.name.indexOf(name) == -1) return false;
        return true;
    });
    let total = mockUsers.length;
    mockUsers = mockUsers.filter((u, index) => index < 20 * page && index >= 20 * (page - 1));
    var result={
            total: total,
            users:mockUsers,
    }
    res.json(result)
}

exports.add=function(req,res,next){
    let { name, addr, age, birth, sex } = req.body;
    _Users.push({
        name: name,
        addr: addr,
        age: age,
        birth: birth,
        sex: sex
    });
    var result={
        code:200,
        msg:'添加成功',
    }
    res.json(result)
}


exports.remove=function(req,res,next){
    let { id } = req.body
    _Users = _Users.filter(u => u.id !== id);
    var result={
        code:200,
        msg:'删除成功',
    }
    res.json(result)
}

exports.edit=function(req,res,next){
    let { id, name, addr, age, birth, sex } = req.bdoy;
    _Users.some(u => {
        if (u.id === id) {
            u.name = name;
            u.addr = addr;
            u.age = age;
            u.birth = birth;
            u.sex = sex;
            return true;
        }
    });
    var result={
        code:200,
        msg:'修改成功',
    }
    res.json(result)
}